﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using Tools;

namespace Punia
{
    [CommandAttribute(true)]
    public partial class create_queue : BaseForm
    {
        public create_queue()
            : base()
        {
        }

        public create_queue(DbContext dbContext)
            : base(dbContext)
        {
            InitializeComponent();

            cbExecuteAs.DataSource = Utility.execute_as(dbContext);
            cbExecuteAs.DisplayMember = "Name";

            cbProcedureName.DataSource = Utility.proceudres(dbContext);
            cbProcedureName.DisplayMember = "Name";

            cbFileGroup.DataSource = Utility.filegroups(dbContext);
            cbFileGroup.DisplayMember = "Name";

            cbSchema.DataSource = Utility.schemas(dbContext);
            cbSchema.DisplayMember = "Name";
        }

        protected override void Write()
        {
            StringBuilder sb = new StringBuilder();

            string schema = "";

            if (cbSchema.SelectedIndex > 0)
            {
                schema = "[" + cbSchema.Text + "].";
            }

            sb.AppendLine("USE [" + DbContext.Database + "];");
            sb.AppendLine();
            sb.AppendLine("CREATE QUEUE " + schema + "[" + tbName.Text + "] ");

            sb.AppendLine("WITH");

            if (!cbStatus.Checked) sb.AppendLine("STATUS = OFF,");
            else sb.AppendLine("STATUS = ON,");

            if (cbActivation.Checked)
            {
                if (cbRetention.Checked)
                {
                    sb.AppendLine("RETENTION = ON,");
                }
                else
                {
                    sb.AppendLine("RETENTION = OFF,");
                }

                sb.AppendLine("ACTIVATION (");

                if (cbActivationStatus.Checked)
                {
                    sb.AppendLine("   STATUS = ON, ");
                }
                else
                {
                    sb.AppendLine("   STATUS = OFF, ");
                }

                sb.AppendLine("   PROCEDURE_NAME = " + cbProcedureName.Text + ", ");

                sb.AppendLine("   MAX_QUEUE_READERS = " + tbMaxQueueReaders.Text + ", ");

                if (cbExecuteAs.Text == "SELF" || cbExecuteAs.Text == "OWNER")
                {
                    sb.AppendLine("   EXECUTE AS " + cbExecuteAs.Text);
                }
                else
                {
                    sb.AppendLine("   EXECUTE AS '" + cbExecuteAs.Text + "'");
                }

                sb.AppendLine(")");
            }
            else
            {
                if (cbRetention.Checked)
                {
                    sb.AppendLine("RETENTION = ON");
                }
                else
                {
                    sb.AppendLine("RETENTION = OFF");
                }
            }

            if (chbFileGroup.Checked)
            {
                if (cbFileGroup.Text == "DEFAULT")
                {
                    sb.AppendLine("ON [DEFAULT]");
                }
                else
                {
                    sb.AppendLine("ON [" + cbFileGroup.Text + "]");
                }
            }

            tbCommand.Text = sb.ToString();
        }

        private void cbStatus_CheckedChanged(object sender, EventArgs e)
        {
            Write();
        }

        private void cbRetention_CheckedChanged(object sender, EventArgs e)
        {
            Write();
        }

        private void cbActivation_CheckedChanged(object sender, EventArgs e)
        {
            flpActivation.Visible = cbActivation.Checked;

            Write();
        }

        private void cbActivationStatus_CheckedChanged(object sender, EventArgs e)
        {
            Write();
        }

        private void cbProcedureName_SelectedIndexChanged(object sender, EventArgs e)
        {
            Write();
        }

        private void tbMaxQueueReaders_TextChanged(object sender, EventArgs e)
        {
            Write();
        }

        private void cbExecuteAs_SelectedIndexChanged(object sender, EventArgs e)
        {
            Write();
        }

        private void chbFileGroup_CheckedChanged(object sender, EventArgs e)
        {
            cbFileGroup.Enabled = chbFileGroup.Checked;

            Write();
        }

        private void cbFileGroup_SelectedIndexChanged(object sender, EventArgs e)
        {
            Write();
        }

        private void tbName_TextChanged(object sender, EventArgs e)
        {
            Write();
        }

        private void cbSchema_SelectedIndexChanged(object sender, EventArgs e)
        {
            Write();
        }
    }
}